using System;
using System.Collections.Generic;
using System.Text;
using ImdbServices;
using System.Collections;
using System.Threading;
using MyCollections.Controls;
using ImdbServices;

namespace MyCollections
{   
    public class ImdbService
    {
        public Imdb imdb = new Imdb();
        private ArrayList moviesResultset = new ArrayList();
        private ArrayList moviesResult = new ArrayList();
        private bool show_ExactMatches;
        private bool show_PopularTitles;
        private bool show_PartialMatches;
        private int totalresults;
        private string[] titlestofind;
        private ListViewMovie listViewMovie;

        public ImdbService()
        {
            imdb.SearchResultsDownloaded +=
                new Imdb.SearchResultsDownloadedEventHandler(SearchResultsDownloaded);
            imdb.MovieInfoDownloaded +=
                new Imdb.MovieInfoDownloadedEventHandler(MovieInfoDownloadedEventHandler);

            show_ExactMatches = false;
            show_PopularTitles = true;
            show_PartialMatches = false;
            totalresults = 0;
        }

        public ImdbService(bool show_ExactMatches, bool show_PopularTitles, bool show_PartialMatches, ListViewMovie listView)
            : this()
        {
            this.listViewMovie = listView;
            this.listViewMovie.Bt_get_data.Click += new System.EventHandler(this.bt_get_data_Click);
            this.show_ExactMatches = show_ExactMatches;
            this.show_PopularTitles = show_PopularTitles;
            this.show_PartialMatches = show_PartialMatches;
        }

        public void FindMovie(string title)
        {
            initializeElements();

            if (!string.IsNullOrEmpty(title))
            {
                imdb.SearchMovieAsync(title, this.show_PartialMatches, this.show_ExactMatches);
            }
        }

        private void initializeElements()
        { 
            this.moviesResultset = new ArrayList();
            this.moviesResult = new ArrayList();
            this.totalresults = 0;
        }

        #region GETS & SETS
        
        #endregion

        #region EventHandlers

        private void bt_get_data_Click(object sender, EventArgs e)
        {
            MyCollections.Model.MovieCollection m = (MyCollections.Model.MovieCollection)this.listViewMovie.Lb_results.SelectedItem;
            imdb.GetMovieInfoAsync(Convert.ToInt64(m.ImdbID), true);
        }
        private void MovieInfoDownloadedEventHandler(ImdbServices.Movie M)
        {
            MyCollections.Model.MovieCollection movie = new MyCollections.Model.MovieCollection(M);
            movie.Validate = true;
            this.listViewMovie.RemoveAllItens();
            this.listViewMovie.FillListbox(movie);
        }

        private void SearchResultsDownloaded(MoviesResultset M)
        {
            if (M == null)
                return;

            this.moviesResultset.Add(M);

            if (this.show_PopularTitles && M.PopularTitles.Count > 0)
            {
                foreach (Movie m in M.PopularTitles)
                {
                    //movies[counter] = m; counter++;
                    //imdb.GetMovieInfoAsync(Convert.ToInt64(m.ImdbID), true);
                    MyCollections.Model.MovieCollection movie = new MyCollections.Model.MovieCollection(m);
                    this.listViewMovie.FillListbox(movie);
                    this.totalresults += 1;
                }
            }
            if (this.show_ExactMatches && M.ExactMatches.Count > 0)
            {
                foreach (Movie m in M.ExactMatches)
                {
                    //movies[counter] = m; counter++;
                    //imdb.GetMovieInfoAsync(Convert.ToInt64(m.ImdbID), true);
                    MyCollections.Model.MovieCollection movie = new MyCollections.Model.MovieCollection(m);
                    this.listViewMovie.FillListbox(movie);
                    this.totalresults += 1;
                }
            }
            if (this.show_PartialMatches && M.PartialMatches.Count > 0)
            {
                foreach (Movie m in M.PartialMatches)
                {
                    //movies[counter] = m; counter++;
                    //imdb.GetMovieInfoAsync(Convert.ToInt64(m.ImdbID), true);
                    MyCollections.Model.MovieCollection movie = new MyCollections.Model.MovieCollection(m);
                    this.listViewMovie.FillListbox(movie);
                    this.totalresults += 1;
                }
            }
        }
        #endregion
    }
}
